words_dir = '/autofs/space/clive_001/users/adriana/GE_SVM/GE_grand_average_results/words/20220519-0840/';
nonwords_dir = '/autofs/space/clive_001/users/adriana/GE_SVM/GE_grand_average_results/nonwords/20220519-0840/';

word_result_files = dir([words_dir, '*.mat']);
nonword_result_files = dir([nonwords_dir, '*.mat']);

%ROIs = {'R-ParsOrb_1', 'R-SFG_2', 'R-SFG_3', 'R-SFG_1', 'R-preCG_2', 'R-preCG_1', 'R-postCG_2', 'R-postCG_4', 'R-postCG_3', 'R-postCG_1', 'R-postCG_5', 'R-AG_1', 'R-LOC_1', 'R-MTG_4', 'R-ITG_2', 'R-STG_2',...
%    'R-MTG_2', 'R-STG_3', 'R-MTG_1', 'R-ITG_1', 'R-ITG_3', 'R-MTG_3', 'R-STG_1', 'L-CC_1', 'L-ParsOrb_1', 'L-ParsTri_1', 'L-cMFG_1', 'L-SFG_1', 'L-postCG_1'...
%    'L-SMG_1', 'L-SPC_1', 'L-ParaHip_1','L-TPol_1', 'L-STG_2', 'L-ITG_2', 'L-MTG_1', 'L-STG_1', 'L-ITG_1', 'L-MTG_2'};


ROIs = {'', 'R-LOC_1', 'R-ParsOrb_1', 'R-SFG_2', 'R-SFG_3', 'R-SFG_1', 'R-preCG_2',...
    'R-preCG_1', 'R-postCG_2', 'R-postCG_4', 'R-postCG_3', 'R-postCG_1', 'R-postCG_5', 'R-AG_1',...
    'R-ITG_2', 'R-ITG_1', 'R-ITG_3', 'R-MTG_4', 'R-MTG_2', 'R-MTG_1', 'R-MTG_3', 'R-STG_2', 'R-STG_3', 'R-STG_1',...
    'L-CC_1', 'L-ParaHip_1', 'L-ParsOrb_1', 'L-ParsTri_1', 'L-cMFG_1', 'L-SFG_1', 'L-postCG_1', 'L-SMG_1',...
    'L-SPC_1', 'L-ITG_2', 'L-ITG_1', 'L-TPol_1', 'L-MTG_1', 'L-MTG_2', 'L-STG_2', 'L-STG_1'};

pubROIs = {'', 'R-LOC_1', 'R-ParsOrb_1', 'R-SFG_3', 'R-SFG_2', 'R-SFG_1', 'R-preCG_2',...
    'R-preCG_1', 'R-postCG_5', 'R-postCG_4', 'R-postCG_3', 'R-postCG_2', 'R-postCG_1', 'R-AG_1',...
    'R-ITG_3', 'R-ITG_2', 'R-ITG_1', 'R-MTG_4', 'R-MTG_3', 'R-MTG_2', 'R-MTG_1', 'R-STG_3', 'R-STG_2', 'R-STG_1',...
    'L-CC_1', 'L-ParaHip_1', 'L-ParsOrb_1', 'L-ParsTri_1', 'L-cMFG_1', 'L-SFG_1', 'L-postCG_1', 'L-SMG_1',...
    'L-SPC_1', 'L-ITG_2', 'L-ITG_1', 'L-TPol_1', 'L-MTG_2', 'L-MTG_1', 'L-STG_2', 'L-STG_1'};

% ROIs = {'R-preCG_2', 'R-postCG_2', 'R-postCG_3', 'R-ITG_2', 'R-ITG_1', 'R-MTG_4',...
%     'R-MTG_2', 'R-MTG_1', 'R-MTG_3', 'R-STG_2', 'R-STG_3', 'R-STG_1', 'L-ParaHip_1',...
%     'L-postCG_1', 'L-SMG_1', 'L-ITG_2', 'L-ITG_1', 'L-MTG_1', 'L-MTG_2', 'L-STG_2', 'L-STG_1'};
% 
% pubROIs = {'R-preCG_2', 'R-postCG_5', 'R-postCG_3', 'R-ITG_3', 'R-ITG_2', 'R-MTG_4',...
%     'R-MTG_3', 'R-MTG_2', 'R-MTG_1', 'R-STG_3', 'R-STG_2', 'R-STG_1', 'L-ParaHip_1',...
%     'L-postCG_1', 'L-SMG_1', 'L-ITG_2', 'L-ITG_1', 'L-MTG_2', 'L-MTG_1', 'L-STG_2', 'L-STG_1'};

time = -100:1000;
ndecoders = 0;
figure('Position', [986, 340, 558, 651]);
area([100, 400], [length(ROIs)+1; length(ROIs)+1], 'FaceColor', [210, 210, 210]./255, 'FaceAlpha', 1); hold on;
area([400, 600], [length(ROIs)+1; length(ROIs)+1], 'FaceColor', [181, 255, 210]./255, 'FaceAlpha', 1); hold on;

for kfile = 1:length(word_result_files)
    file = word_result_files(kfile);
    load(fullfile(words_dir,file.name));
    if ~isempty(stat_ave.clusters)
        fnameparts = split(file.name, {'_', '-'});
        roiname = sprintf('%s-%s_%s', fnameparts{3}, fnameparts{4}, fnameparts{5});
        ndecoders = find(ismember(ROIs, roiname));
        for kcluster = 1:length(stat_ave.clusters)
            timestart = time(stat_ave.clusters{kcluster}(1));
            timeend = time(stat_ave.clusters{kcluster}(end));
            plot([timestart, timeend], [ndecoders+0.2, ndecoders+0.2], '|-', 'Color', [30 136 229]./255, 'LineWidth', 2.5); hold on;
        end
    end
end


for kfile = 1:length(nonword_result_files)
    file = nonword_result_files(kfile);
    load(fullfile(nonwords_dir, file.name));
    if ~isempty(stat_ave.clusters)
        fnameparts = split(file.name, {'_', '-'});
        roiname = sprintf('%s-%s_%s', fnameparts{3}, fnameparts{4}, fnameparts{5});
        ndecoders = find(ismember(ROIs, roiname));
        for kcluster = 1:length(stat_ave.clusters)
            timestart = time(stat_ave.clusters{kcluster}(1));
            timeend = time(stat_ave.clusters{kcluster}(end));
            plot([timestart, timeend], [ndecoders-0.2, ndecoders-0.2], '|:', 'Color', [216, 27, 96]./255, 'LineWidth', 2.5); hold on;
        end
    end
end

%set(gca, 'TickLabelInterpreter', 'none');
set(gca, 'YLim', [0, length(ROIs)+1], 'YTick', 2:length(ROIs), 'YTickLabels', pubROIs(2:end));
set(gca, 'XLim', [-100, 1000]);
plot([350, 350], [0, length(ROIs)+1], 'k--', 'LineWidth', 1);
text(350, 1, '<-pre-offset ', 'HorizontalAlignment', 'right');
text(350, 1, ' post-offset->', 'HorizontalAlignment', 'left');
xlabel('Time (ms)');

late_nonsig = {'L-ITG_2'};
late_sig = {'L-postCG_1', 'R-postCG_2', 'R-ITG_2', 'R-STG_2', 'R-MTG_2', 'R-STG_3', 'R-STG_1', 'R-ITG_1', 'R-MTG_1', 'L-STG_1', 'L-MTG_2'};
early_nonsig = {'L-postCG_1'};
early_sig = {'R-preCG_2', 'R-postCG_2', 'R-ITG_2', 'R-ITG_1', 'R-MTG_4', 'R-MTG_2', 'R-MTG_1', 'R-MTG_3', 'R-STG_2', 'R-STG_1', 'R-STG_3', 'L-ParaHip_1', 'L-SMG_1', 'L-ITG_1', 'L-MTG_2', 'L-MTG_1', 'L-STG_1'};
for k = 1:length(early_nonsig)
    text(250, find(ismember(ROIs, early_nonsig(k))), 'n.s.', 'HorizontalAlignment', 'right');
end

for k = 1:length(late_nonsig)
    text(500, find(ismember(ROIs, late_nonsig(k))), 'n.s.', 'HorizontalAlignment', 'center');
end

asteriskTime = [repmat(250, 1, length(early_sig)), repmat(500, 1, length(late_sig))];
asteriskROI = [find(ismember(ROIs, early_sig)), find(ismember(ROIs, late_sig))];
scatter(asteriskTime, asteriskROI, [], 'k', '*');
h1 = plot([1000, 2000], [100, 100], '-', 'Color', [30 136 229]./255, 'LineWidth', 2.5);
h2 = plot([1000, 2000], [100, 100], ':', 'Color', [216,27,96]/255, 'LineWidth', 2.5);
legend([h1, h2], {'Words', 'Nonwords'}, 'location', 'northeast');